import { IFeidaoAiBrowserComponent } from '@feidao/web/interfaces';
import s from './s';
import init from '@feidao/web/framework/browser/dn/component';
import fire_component_msg from '@feidao/web/atom/browser/fsm/fire-component-msg';
import nodejs from '@feidao/web/atom/nodejs/msg-b/nodejs';
import render from '@feidao/web/atom/browser/render/render';
import P01 from './P01';
import P02 from './P02';
const feidao = {};
let fzno = 0;
const daoke = {
	async init(fd: IFeidaoAiBrowserComponent, ...args: any[]) {
		const count_input = fd.data.node.querySelector('[data-feidao-count]') as HTMLInputElement;
		if (count_input) {
			fzno = Number(count_input.value);
		}
	},
	async eu01(fd: IFeidaoAiBrowserComponent, ...args: any[]) {
		console.log('zj-reality-picture-001 init b click 新增', args);
		fire_component_msg('zj-reality-picture-002', 'ec01', fzno + 1);
	},
	async eu02(fd: IFeidaoAiBrowserComponent, ...args: any[]) {
		console.log('zj-reality-picture-001  click 看大图', args);
		const idx = args[0].currentTarget.dataset.feidaoIdx;
		const pic = args[0].currentTarget.dataset.feidaoPic;
		const classroom_id = fd.data.params.classroom_id;
		console.log(classroom_id);
		const query = {
			criteria: { classroom_id },
			asc: ['fzno']
		};
		fire_component_msg('zj-reality-picture-003', 'ec01', idx, pic, query);
	},
	async eu03(fd: IFeidaoAiBrowserComponent, ...args: any[]) {
		console.log('zj-reality-picture-001 click 删除', args);
		const _id = args[0].currentTarget.dataset.feidaoId;
		console.log(JSON.stringify({ rowdata: { _id } }));
		const res = await nodejs("flippedclass", "class_room_picture_del", { rowdata: { _id } });
		if (res == 'success') {
			alert("操作成功");
			fd.fire('ec04', 'del');
		} else {
			alert("操作失败");
		}
	},
	async ec04(fd: IFeidaoAiBrowserComponent, ...args: any[]) {
		console.log('zj-reality-picture-001 click 刷新', args);
		if (args[0] && args[0][0] == 'add') {
			fzno++;
		} else if (args[0] && args[0][0] == 'del') {
			// fzno--;
		}
		const classroom_id = fd.data.params.classroom_id;
		const query = {
			criteria: { classroom_id },
			asc: ['fzno']
		};
		const res = await nodejs("flippedclass", "class_room_picture_find", query);
		if (res.length == 0) {
			render(fd, '', P02, 'P01', 'inner');
		} else {
			render(fd, res, P01, 'P01', 'inner');
		}
	}
};

const rules = {};

export default function main(url: string, query: any) {
	return init('zj-reality-picture-001', s, rules, feidao, daoke, url, query, { spaceid: 'flippedclass', systemid: '5991', refsystemid: '@refsystemid' });
}
